package ar.com.jendrix.common.web;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import ar.com.jendrix.common.exception.ApplicationException;
import ar.com.jendrix.common.exception.SystemException;

@SuppressWarnings("serial")
public abstract class BaseBean implements Serializable {

	protected Logger log = Logger.getLogger(getClass());

	@PostConstruct
	protected final void init() {
		log.info("init " + getClass().getSimpleName());
		try {
			doInit();
			clear();
		} catch (Throwable t) {
			log.error(t.getMessage(), t);
			addErrorMessage("Error al inicializar pagina");
		}
	}

	protected void doInit() throws SystemException {
		initManagerLocal();
		initWebComponent();
		initSelectItemList();
		initImage();
	}

	/**
	 */
	protected void initManagerLocal() throws SystemException {
	}

	/**
	 */
	protected void initWebComponent() throws SystemException {
	}

	/**
	 */
	protected void initSelectItemList() throws SystemException {
	}

	/**
	 */
	protected void initImage() throws SystemException {
	}

	/**
	 */
	protected void clear() throws ApplicationException, SystemException {
		log.info("clear " + getClass().getSimpleName());
	}

	/**
	 * 
	 * @param message
	 */
	protected final void addInfoMessage(String message) {
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Información", message));
	}

	/**
	 * 
	 * @param message
	 */
	protected final void addInfoMessages(List<String> messages) {
		for (String message : messages) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Información", message));
		}
	}

	/**
	 * 
	 * @param message
	 */
	protected final void addWarningMessage(String message) {
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Atención", message));
	}

	/**
	 * 
	 * @param message
	 */
	protected final void addWarningMessages(List<String> messages) {
		for (String message : messages) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Atención", message));
		}
	}

	/**
	 * 
	 * @param message
	 */
	protected final void addErrorMessage(String message) {
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", message));
	}

	/**
	 * 
	 * @param message
	 */
	protected final void addErrorMessages(List<String> messages) {
		for (String message : messages) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", message));
		}
	}

	/**
	 * 
	 * @param message
	 */
	protected final void addValidationMessage(String message) {
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Validación", message));
	}

	/**
	 * 
	 * @param message
	 */
	protected final void addValidationMessages(List<String> messages) {
		for (String message : messages) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Validación", message));
		}
	}

	/**
	 * 
	 * @param message
	 */
	protected final void addCriticalMessage(String message) {
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Crítico", message));
	}

	/**
	 * 
	 * @param message
	 */
	protected final void addCriticalMessages(List<String> messages) {
		for (String message : messages) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Crítico", message));
		}
	}
}